﻿#include "delegatecenter.h"
#include <QApplication>
#include <QProgressBar>

EnumDelegate::EnumDelegate(QStringList strList, QObject *parent):QItemDelegate(parent)
{
    enumStringList = strList;
}

void EnumDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
{
    QVariant value = index.data(Qt::FontRole);
    if(value.isValid() && !value.isNull()){
        option->font = qvariant_cast<QFont>(value).resolve(option->font);
        option->fontMetrics = QFontMetrics(option->font);
    }
    //对齐方式
    value = index.data(Qt::TextAlignmentRole);
    if(value.isValid() && !value.isNull())
        option->displayAlignment = Qt::Alignment(value.toInt());

    option->displayAlignment = Qt::AlignCenter; //居中
    //前景色
    value = index.data(Qt::ForegroundRole);
    if(value.canConvert<QBrush>())
        option->palette.setBrush(QPalette::Text,qvariant_cast<QBrush>(value));

    option->features |= QStyleOptionViewItem::HasDisplay;
    option->text = getDisplayText(index);

}

QString EnumDelegate::getDisplayText(const QModelIndex &index) const
{
    int enumNum = index.data().toInt();
    if(enumNum >= enumStringList.size() || enumNum < 0)
        return QString("未知");
    else
        return enumStringList.at(enumNum);
}

void EnumDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid()){
        Q_ASSERT(index.isValid());

        QStyleOptionViewItem opt = option;
        //去掉焦点
        opt.state &= ~QStyle::State_HasFocus;
        initStyleOption(&opt,index);
        const QWidget *widget = opt.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem,&opt,painter,widget);
    }else{
        QItemDelegate::paint(painter,option,index);
    }
}

NumberDelegate::NumberDelegate(int m_precision, QObject *parent)
{
    precision = m_precision;
}

void NumberDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
{
    QVariant value = index.data(Qt::FontRole);
    if(value.isValid() && !value.isNull()){
        option->font = qvariant_cast<QFont>(value).resolve(option->font);
        option->fontMetrics = QFontMetrics(option->font);
    }
    //对齐方式
    value = index.data(Qt::TextAlignmentRole);
    if(value.isValid() && !value.isNull())
        option->displayAlignment = Qt::Alignment(value.toInt());

    //option->displayAlignment = Qt::AlignCenter; 居中
    //前景色
    value = index.data(Qt::ForegroundRole);
    if(value.canConvert<QBrush>())
        option->palette.setBrush(QPalette::Text,qvariant_cast<QBrush>(value));

    option->features |= QStyleOptionViewItem::HasDisplay;
    option->text = getDisplayText(index);
}

QString NumberDelegate::getDisplayText(const QModelIndex &index) const
{
    double val = index.data().toDouble();
    return QString::number(val,'f',precision);
}

void NumberDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid()){
        Q_ASSERT(index.isValid());

        QStyleOptionViewItem opt = option;
        //去掉焦点
        opt.state &= ~QStyle::State_HasFocus;
        initStyleOption(&opt,index);
        const QWidget *widget = opt.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem,&opt,painter,widget);
    }else{
        QItemDelegate::paint(painter,option,index);
    }
}

ProgressDelegate::ProgressDelegate(QObject *parent)
{

}

void ProgressDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
{
    QVariant value = index.data(Qt::FontRole);
    if(value.isValid() && !value.isNull()){
        option->font = qvariant_cast<QFont>(value).resolve(option->font);
        option->fontMetrics = QFontMetrics(option->font);
    }
    //对齐方式
    value = index.data(Qt::TextAlignmentRole);
    if(value.isValid() && !value.isNull())
        option->displayAlignment = Qt::Alignment(value.toInt());

    //option->displayAlignment = Qt::AlignCenter; 居中
    //前景色
    value = index.data(Qt::ForegroundRole);
    if(value.canConvert<QBrush>())
        option->palette.setBrush(QPalette::Text,qvariant_cast<QBrush>(value));

    option->features |= QStyleOptionViewItem::HasDisplay;
    option->text = getDisplayText(index);
}

QString ProgressDelegate::getDisplayText(const QModelIndex &index) const
{
    return QString();
}

void ProgressDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid()){
        Q_ASSERT(index.isValid());

        QStyleOptionViewItem opt = option;
        //去掉焦点
        opt.state &= ~QStyle::State_HasFocus;
        //initStyleOption(&opt,index);
        const QWidget *widget = opt.widget;
        QStyle *style = QApplication::style();
        QStyleOptionProgressBar progressBar;
        progressBar.rect = opt.rect;
        progressBar.minimum = 0;
        progressBar.maximum = 100;
        progressBar.progress = index.data().toInt();
        progressBar.textVisible = true;
        //progressBar.textAlignment = Qt::AlignCenter;
        progressBar.text = QString("%1%").arg(progressBar.progress);
        style->drawControl(QStyle::CE_ProgressBar,&progressBar,painter,widget);
    }else{
        QItemDelegate::paint(painter,option,index);
    }
}


ByteDelegate::ByteDelegate(QObject *parent)
{

}

void ByteDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
{
    QVariant value = index.data(Qt::FontRole);
    if(value.isValid() && !value.isNull()){
        option->font = qvariant_cast<QFont>(value).resolve(option->font);
        option->fontMetrics = QFontMetrics(option->font);
    }
    //对齐方式
    value = index.data(Qt::TextAlignmentRole);
    if(value.isValid() && !value.isNull())
        option->displayAlignment = Qt::Alignment(value.toInt());

    option->displayAlignment = Qt::AlignRight |  Qt::AlignVCenter; //居右
    //前景色
    value = index.data(Qt::ForegroundRole);
    if(value.canConvert<QBrush>())
        option->palette.setBrush(QPalette::Text,qvariant_cast<QBrush>(value));

    option->features |= QStyleOptionViewItem::HasDisplay;
    option->text = getDisplayText(index);
}

QString ByteDelegate::getDisplayText(const QModelIndex &index) const
{
    double val = index.data().toDouble();
    QString strUnit;
    double dSize = val * 1.0;
    int precision = 2;
    if (dSize <= 0)
    {
        dSize = 0.0;
        precision = 0;
    }
    else if (dSize < 1024)
    {
        strUnit = "B";
        precision = 0;
    }
    else if (dSize < 1024 * 1024)
    {
        dSize /= 1024;
        strUnit = "KB";
    }
    else if (dSize < 1024 * 1024 * 1024)
    {
        dSize /= (1024 * 1024);
        strUnit = "MB";
    }
    else if (dSize < 1024 * 1024 * 1024 * 1024.0)
    {
        dSize /= (1024 * 1024 * 1024);
        strUnit = "GB";
    }
    else
    {
        dSize /= (1024 * 1024 * 1024 * 1024.0);
        strUnit = "TB";
    }
    return QString("%1 %2").arg(QString::number(dSize, 'f', precision)).arg(strUnit);
}

void ByteDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid()){
        Q_ASSERT(index.isValid());

        QStyleOptionViewItem opt = option;
        //去掉焦点
        opt.state &= ~QStyle::State_HasFocus;
        initStyleOption(&opt,index);
        const QWidget *widget = opt.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem,&opt,painter,widget);
    }else{
        QItemDelegate::paint(painter,option,index);
    }
}


DivisionDelegate::DivisionDelegate(int m_interval, int m_precision, QObject *parent)
{
    interval = m_interval;
    precision = m_precision;
}

void DivisionDelegate::initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
{
    QVariant value = index.data(Qt::FontRole);
    if(value.isValid() && !value.isNull()){
        option->font = qvariant_cast<QFont>(value).resolve(option->font);
        option->fontMetrics = QFontMetrics(option->font);
    }
    //对齐方式
    value = index.data(Qt::TextAlignmentRole);
    if(value.isValid() && !value.isNull())
        option->displayAlignment = Qt::Alignment(value.toInt());

    option->displayAlignment = Qt::AlignRight |  Qt::AlignVCenter; //居右
    //前景色
    value = index.data(Qt::ForegroundRole);
    if(value.canConvert<QBrush>())
        option->palette.setBrush(QPalette::Text,qvariant_cast<QBrush>(value));

    option->features |= QStyleOptionViewItem::HasDisplay;
    option->text = getDisplayText(index);
}

QString DivisionDelegate::getDisplayText(const QModelIndex &index) const
{
    double val = index.data().toDouble();
    QString strVal = QString::number(val, 'f', precision);
    QString outStr = "";
    int num = 1;
    int startPos = strVal.length()-1;
    if(strVal.lastIndexOf('.') != -1){
        outStr = strVal.right(strVal.length()-strVal.lastIndexOf('.'));
        startPos = strVal.lastIndexOf('.')-1;
    }
    for(int pos = startPos;pos >= 0;pos--){
        outStr.insert(0,strVal.at(pos));
        if(num % interval == 0 & pos !=0){
            outStr.insert(0,',');
        }
        num ++;
    }
    return outStr;
}

void DivisionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    if(index.isValid()){
        Q_ASSERT(index.isValid());

        QStyleOptionViewItem opt = option;
        //去掉焦点
        opt.state &= ~QStyle::State_HasFocus;
        initStyleOption(&opt,index);
        const QWidget *widget = opt.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem,&opt,painter,widget);
    }else{
        QItemDelegate::paint(painter,option,index);
    }
}

